{% extends 'base.html' %}

{% block title %}{{ inventory.name }} - 盘查详情{% endblock %}

{% block content %}
<div class="d-flex justify-content-between align-items-center mb-4">
    <h1>{{ inventory.name }} - 盘查详情</h1>
    <div>
        <a href="{{ url_for('inventory.edit_inventory', id=inventory.id) }}" class="btn btn-outline-primary me-2">编辑</a>
        <a href="{{ url_for('inventory.inventory_list') }}" class="btn btn-secondary">返回列表</a>
    </div>
</div>

<!-- 基本信息卡片 -->
<div class="card mb-4">
    <div class="card-header bg-primary text-white">
        <h5 class="mb-0">基本信息</h5>
    </div>
    <div class="card-body">
        <div class="row">
            <div class="col-md-6">
                <p><strong>盘查事项名称：</strong>{{ inventory.name }}</p>
                <p><hidden>时间范围：</strong><hidden>{{ inventory.start_time }} 至 {{ inventory.end_time }}</p>
                <p><strong>组织边界：</strong>{{ inventory.organization_boundary }}</p>
                <p><strong>基准年：</strong>{{ inventory.base_year }}</p>
                <p><strong>所属年份：</strong>{{ inventory.belong_year }}</p>
                <p><strong>所属月份：</strong>{{ inventory.belong_month }}</p>
            </div>
            <div class="col-md-6">
                <p><strong>核算标准：</strong>{{ inventory.accounting_standard }}</p>
                <p><strong>核算方式：</strong>{{ inventory.accounting_method }}</p>
                <p><strong>模型分类：</strong>{{ inventory.model_category_name }}</p>
                <p><strong>盘查目录：</strong>{{ inventory.inventory_category_name }}</p>
                <p><strong>状态：</strong>
                    <span class="badge {% if inventory.status == '新建' %}bg-secondary{% elif inventory.status == '执行' %}bg-primary{% elif inventory.status == '完成' %}bg-success{% else %}bg-danger{% endif %}">
                        {{ inventory.status }}
                    </span>
                </p>
            </div>
        </div>
    </div>
</div>

<!-- 关联排放源列表 -->
<div class="card">
    <div class="card-header d-flex justify-content-between align-items-center">
        <h5 class="mb-0">关联排放源列表</h5>
        <button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addSourceModal">
            新增排放源
        </button>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-striped table-hover">
                <thead>
                    <tr>
                        <th>排放源名称</th>
                        <th>设施</th>
                        <th>说明</th>
                        <th>数据来源</th>
                        <th>单位</th>
                        <th>碳排放因子</th>
                        <th>活动数据</th>
                        <th>值</th>
                        <th>填报人</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for source in emission_sources %}
                    <tr>
                        <td>{{ source.emission_source }}</td>
                        <td>{{ source.facility|default('-') }}</td>
                        <td>{{ source.description|default('-') }}</td>
                        <td>{{ source.data_source|default('-') }}</td>
                        <td>{{ source.emission_unit }}</td>
                        <td>
                            <a href="{{ url_for('factor.factor_list') }}" target="_blank">{{ source.factor_name }}</a>
                        </td>
                        <td>{{ source.activity_data|default('-') }}</td>
                        <td>
                            {% if source.activity_data and source.emission_value %}
                                {{ (source.activity_data * source.emission_value)|round(2) }}
                            {% else %}
                                -
                            {% endif %}
                        </td>
                        <td>{{ source.reporter|default('-') }}</td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <button class="btn btn-outline-primary" data-bs-toggle="modal" data-bs-target="#editSourceModal{{ source.id }}">编辑</button>
                                <form action="{{ url_for('inventory.delete_source', id=source.id) }}" method="post" class="ms-1">
                                    <button type="submit" class="btn btn-outline-danger" onclick="return confirm('确定删除此排放源吗？')">删除</button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% else %}
                    <tr>
                        <td colspan="10" class="text-center">暂无排放源数据</td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</div>

<!-- 新增排放源模态框 -->
<div class="modal fade" id="addSourceModal" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">新增排放源</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <form id="addSourceForm" action="{{ url_for('inventory.add_source', inventory_id=inventory.id) }}" method="post">
                    <div class="mb-3">
                        <label class="form-label">选择排放源</label>
                        <select class="form-select" name="source_ids" multiple size="10" required>
                            {% for model in all_models %}
                            <option value="{{ model.id }}">{{ model.emission_source }}</option>
                            {% endfor %}
                        </select>
                        <small class="text-muted">按住Ctrl键可多选</small>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <button type="submit" form="addSourceForm" class="btn btn-primary">添加</button>
            </div>
        </div>
    </div>
</div>
{% endblock %}